#include "LoaderConfig.h"

LoaderConfig::LoaderConfig(void)
{
}

LoaderConfig::~LoaderConfig(void)
{
}

LoaderConfig::LoaderConfig(std::string const &nameFile)
{
	this->loadFile(nameFile);	
}


void LoaderConfig::loadFile(std::string const &nameFile)
{
	std::ifstream fileTemp(nameFile);
	std::string tempLine = "";

	if (!fileTemp.is_open())
		throw ServerException("Impossible to find the file '" + nameFile + "'.");

	while(getline(fileTemp, tempLine, '\n'))
	{
		try
		{
			int pos = tempLine.find('=');

			if (pos != -1)
			{
				std::pair<std::string, std::string> tpair;
				tpair.first = tpair.second = "";
				
				tpair.first = tempLine.substr(0, pos);
				tpair.second = tempLine.substr(pos+1, tempLine.size());

				m_config[tpair.first] = tpair.second; 
			}
		}
		catch(...)
		{
			LogController::getInstance()->AddToLog(LogController::L_WARNING, "Error parsing config file");
		}
	}
}


std::string LoaderConfig::getValue(std::string const &key)
{
	for(std::map<std::string, std::string>::iterator it = m_config.begin(); it != m_config.end(); it++)
	{
		if (it->first == key)
			return it->second;
	}
	return "";
}

std::string LoaderConfig::getKey(std::string const &value)
{
	for(std::map<std::string, std::string>::iterator it = m_config.begin(); it != m_config.end(); it++)
	{
		if (it->second == value)
			return it->first;
	}
	return "";
}

